home *** CD-ROM | disk | FTP | other *** search
/ Amiga Packmags / NewsFlash - Issue 07 (1990-01)(UGA - 17-Bit Software)(Disk 1 of 2)[a].zip / NewsFlash - Issue 07 (1990-01)(UGA - 17-Bit Software)(Disk 1 of 2)[a].adf / doc / CTutorial.doc < prev    next >
Text File  |  1988-01-19  |  15KB  |  423 lines

  1.  
  2.                            ALL AT C
  3.                            --------
  4.  
  5.                           By Andy P++
  6.  
  7.  
  8.  
  9. Do you want to learn to  program  in C?  Or maybe you would
  10. like to  understand  the  Intuition  Reference  manual?  Or
  11. perhaps you  are  just  curious  about  why  the  gurus  at
  12. Commodore decided it would be a  "good  thing" to use C for
  13. major parts of  the  software  development  for  the Amiga.
  14. Then read on.
  15.  
  16. In this series of  articles  I  hope  to  introduce all the
  17. major features of C in the  context of the Amiga.  I assume
  18. that most readers are familiar  with  at least some form of
  19. programming... even BASIC!
  20.  
  21. What is C?
  22. ----------
  23.  
  24.         Wow! the tricky stuff first.   Well, C is the third
  25.         letter of the alphabet  (our  alphabet anyway), but
  26.         it is also a name  for a programming language.  The
  27.         language was developed  by  a  small  team  at Bell
  28.         Laboratories.  It was created in response to a need
  29.         for a language to implement a new operating system,
  30.         UNIX.
  31.  
  32.         The  language  itself  was   based  on  a  previous
  33.         language developed by Ken Thompson in 1970 called B
  34.         (hence C...  now you know!).   B  itself owed a lot
  35.         to  a  previous   language,   BCPL,   developed  at
  36.         Cambridge University in  1967  by  Martin Richards.
  37.         BCPL stands  for  Basic  CPL  (Combined Programming
  38.         Language).  It is a curious coincidence that, apart
  39.         from assembler, the Amigas  system software was all
  40.         written either in C or BCPL!
  41.  
  42. OK, but what makes C different from BASIC or assembler?
  43. -------------------------------------------------------
  44.  
  45.         Lots, but it may  help  to  categorise  C as a low-
  46.         level language, some  have  even  said a structured
  47.         assembler.
  48.  
  49.         BASIC is a much higher  level language in the sense
  50.         that the  user  is  much  more  insulated  from the
  51.         hardware.  You BASIC  programmers  out there (there
  52.         are a few left I've been  told) are going to have a
  53.         much harder time with C than  those of you who have
  54.         hacked it at the machine level.
  55.  
  56.         Another thing that  distinguishes  C  from BASIC is
  57.         that C is  almost  exclusively  compiled.   That is
  58.         that the program is  written first, then translated
  59.         to  machine  code,   then   run.    I   say  almost
  60.         exclusively  since  I   have   seen  interpreted  C
  61.         advertised... this seems  a  case  of  the worst of
  62.         both worlds!
  63.  
  64.         Although closer in spirit  to  assembler, C is more
  65.         of  a   symbolic   language,   representing   basic
  66.         operations in a (nearly) portable way.
  67.  
  68. OK, Quit the bovine excrement, show us a program
  69. ------------------------------------------------
  70.  
  71.         Alright, you asked for it...
  72.  
  73.                 main()
  74.                 {
  75.                         printf( "Hello world\n");
  76.                 }
  77.  
  78.         That was a complete  program  to output the message
  79.         'Hello world' followed by a  newline to the screen.
  80.         To BASIC boys, yes I  know  it's  a bit verbose, to
  81.         assembler hackers, yes I know it's a bit brief.
  82.  
  83.         Although easy to write you  will go through 7 kinds
  84.         of hell trying to compile  and  run  this.  It is a
  85.         characteristic of  this  type  of  programming  (as
  86.         opposed to BASIC) that a short program is nearly as
  87.         tricky as a medium sized one.
  88.  
  89.         Let me try to explain the elements of this "simple"
  90.         program.
  91.  
  92.         The word  "main()"  introduces  the  start  of  the
  93.         program, it says "this is  the  first thing to do".
  94.         Later I  will  explain  about  functions,  and  the
  95.         meaning of the empty brackets should become clear.
  96.  
  97.         The curly brackets "{" and  "}" are used to collect
  98.         together a set of  things,  in  this  case a set of
  99.         "statements" (equivalent to lines of basic code, or
  100.         lines of assembler).
  101.  
  102.         The only statement in this program is:
  103.                  printf("Hello World\n");
  104.  
  105.         This says print the text  'Hello world' followed by
  106.         a newline ( '\n' is C-speak for a newline).
  107.  
  108.  
  109. Most impressive, but there must be more
  110. ---------------------------------------
  111.  
  112.         Well yes, but to explain  much  more I will have to
  113.         go through a few fundamentals  of C.  At this point
  114.         things will probably seem to  be a bit disconnected
  115.         but I will try to  show  an  example  at the end to
  116.         draw the threads together.
  117.  
  118.         Data types
  119.         ----------
  120.  
  121.         C can handle various sorts  of data.  The principle
  122.         simple types are integers  (0,1,2  etc, even -1, -2
  123.         and so on if you can  handle that), characters ( A,
  124.         B, C and so on) and floating point numbers (24.678,
  125.         0.00045, and many  others).   These  are  the basic
  126.         sorts of thing that C deals with, and it has a name
  127.         for each of them.
  128.  
  129.         There  are  other   types   but   they  are  either
  130.         variations  on  a  theme  (for  example  signed  or
  131.         unsigned numbers),  or  collections  of  the simple
  132.         types (arrays etc.).
  133.  
  134.         In common with assembler, C insists that the person
  135.         writing the program  tells  the  compiler about the
  136.         data used,  its  name  and  its  type.   Such named
  137.         pieces of  data  are  called  "variables",  each of
  138.         which can contain a  range  of values determined by
  139.         the type.
  140.  
  141.         The predefined simple types are:
  142.  
  143.         TYPE    DESCRIPTION                     RANGE
  144.  
  145.         int     signed integers         -2147483648 to
  146.                                                 2147483647
  147.  
  148.         char    a character or byte      -128 to 127
  149.  
  150.         float   a "real" number         +/-10E-37 to
  151.                                                 +/-10E+38
  152.  
  153.         A few explanations here...
  154.  
  155.         Characters  are  represented  by   a  small  number
  156.         corresponding to the ASCII code, eg 'A' is 65.
  157.  
  158.         For the assembler folks, an int is held in 32 bits,
  159.         a char in 8 bits and a float in 32 bits.
  160.  
  161.         Until further notice I  will  ignore  floats, as in
  162.         many years of programming I have very rarely needed
  163.         them.
  164.  
  165.         Given these types, if you  want  to name a variable
  166.         you simply precede the  name  you  want to use with
  167.         the name of the type, for example:
  168.  
  169.                 int     a;
  170.  
  171.         Says the variable  'a'  is  an  integer.   C allows
  172.         quite a lot  of  flexibility  in  naming variables,
  173.         lower case, upper  case,  numbers  and underscores,
  174.         although  names  must  start   with   a  letter  or
  175.         underscore.
  176.  
  177.         So:
  178.  
  179.                 fred, Quite_A_Long_Name, A12343, _43
  180.  
  181.         are all OK names.
  182.  
  183.                 1Henry, 128, &fred, xy#z
  184.  
  185.         would all be rejected.
  186.  
  187.  
  188.         Statements
  189.         ----------
  190.  
  191.         As well as data, a complete program uses statements
  192.         to tell the computer what to do.
  193.  
  194.         In C there  are  a  range  of  statement  types, we
  195.         introduce a couple here.
  196.  
  197.         Assignment:
  198.  
  199.             To change the value  of  a  variable we usually
  200.             use an  assignment  statement.   This  uses the
  201.             equals sign.  Assume we  have a variable called
  202.             'nissan' that is of type  'int', that is it can
  203.             contain integers.  Then to  put  the value 4096
  204.             in it we use the statement:
  205.  
  206.                  nissan = 4096;
  207.  
  208.             The semicolon says, "this  is  the  end of this
  209.             statement".
  210.  
  211.             What comes after the '='  sign does not have to
  212.             be a single thing, it could be an expression:
  213.  
  214.                  nissan = 4096 + 17;
  215.  
  216.             or  even   reference   another   variable,  say
  217.             'mitsubishi':
  218.  
  219.                  nissan = 4096 - mitsubishi;
  220.  
  221.  
  222.         While loop:
  223.  
  224.             The simplest loop in C is the "while" loop.  It
  225.             allows a statement or group of statements to be
  226.             repeated while some condition is true.
  227.  
  228.  
  229.             This looks like:
  230.  
  231.                  while( condition)
  232.                          statement;
  233.  
  234.             Or if we want  to  loop  through  more than one
  235.             statement use the curly brackets:
  236.  
  237.                  while( condition)
  238.                  {
  239.                          statement1;
  240.                          statement2;
  241.                  }
  242.  
  243.  
  244.         Comments
  245.         --------
  246.  
  247.         To help make C code  more  readable (some would say
  248.             "less    unreadable"!)    comments    may    be
  249.             interspersed with the code at most places where
  250.             a space could be used.
  251.  
  252.         A comment is  distinguished  by  being  enclosed in
  253.             comment brackets,  /*  ....  */.   The compiler
  254.             ignores  comments  entirely,  and  unlike  some
  255.             interpreted  languages  comments  do  not  slow
  256.             down, or make bigger, your program so feel free
  257.             to express yourself.
  258.  
  259.  
  260. A more complete example
  261. -----------------------
  262.  
  263.         Using what we now know, lets write a simple program
  264.             to print out the numbers  1  to 5 and the cubes
  265.             of these numbers.
  266.  
  267.         main()  /* Remember, this says start here */
  268.         {
  269.             /* Declare an integer variable */
  270.             int a_number;
  271.             /* And another for its cube */
  272.             int cube;
  273.  
  274.             /* Initialise a number */
  275.             a_number = 1;
  276.  
  277.             while ( a_number <= 5)
  278.             {
  279.                 /* Calculate the cube */
  280.                 cube = a_number * a_number * a_number;
  281.  
  282.                 /* And print the result */
  283.                 printf("%d cubed is %d\n", a_number, cube);
  284.  
  285.                 /* Go on to the next number */
  286.                 a_number = a_number + 1;
  287.             }
  288.         }
  289.  
  290.         Quick word of  explanation  on  the  "printf" line.
  291.         "printf" is the nearest equivalent  C has to BASICs
  292.         PRINT command.  It is rather  more  ugly to look at
  293.         but can do very  similar  (and  in  some cases more
  294.         sophisticated)  things.   Rather  than  the  simple
  295.         print of  a  string  we  did  before,  now  we  are
  296.         printing numbers  inside  a  string.   Wherever the
  297.         "%d" is found, the  next  number  in  the list that
  298.         follows is displayed.
  299.  
  300.         The output  of  that  program  will  therefore look
  301.         like:
  302.  
  303.             1 cubed is 1
  304.             2 cubed is 8
  305.             3 cubed is 27
  306.             4 cubed is 64
  307.             5 cubed is 125
  308.  
  309.  
  310.         That seemed a bit windy, there will be those of you
  311.         out there saying, "Yes, but  I  thought C was terse
  312.         and obscure".  You would  of  course  be right in a
  313.         sense as  it  largely  depends  on  the programmer.
  314.         Putting my  C-hacker  hat  on  we  can  rewrite the
  315.         previous program as:
  316.  
  317.             main()
  318.             {
  319.                 int i=0;
  320.  
  321.                 while( ++i < 6)
  322.                     printf( "%d cubed is %d\n", i, i*i*i);
  323.             }
  324.  
  325.         This introduces only one  really new concept (apart
  326.         from the  fact  that  C  programmers  take  an  odd
  327.         pleasure in being needlessly  obscure)  and that is
  328.         the increment operator, the double plus sign.
  329.  
  330.         This operator is worthy  of  a  place in the "black
  331.         museum"  of  programming.   Its   advantage  is  in
  332.         producing  very  terse  programs  that  are  easily
  333.         translated to  quite  terse  object  programs.  Its
  334.         disadvantage is  that  badly  used  it  can  make a
  335.         program very difficult to understand.
  336.  
  337.         The effect of  the  ++  operator  is twofold.  When
  338.         placed in front of a variable  it first adds one to
  339.         the variable,  and  then  returns  that incremented
  340.         value.
  341.  
  342.         Assembler  programmers  will   understand   the  ++
  343.         operator  as  being   very   similar   to   an  INC
  344.         instruction.  Most high level languages do not have
  345.         a similar concept so the C statement:
  346.  
  347.                     charlie = ++fred;
  348.  
  349.             would in most  ordinary  languages be something
  350.         like:
  351.  
  352.                     fred = fred + 1;
  353.                     charlie = fred;
  354.  
  355.         Note that both examples there  were in fact valid C
  356.         and if you don't like them you can always avoid the
  357.         use of the  ++  operator.   Unfortunately  you will
  358.         probably need to read code using it.
  359.  
  360.         Before I wrap up this  first lesson (watch out K&R,
  361.         here I come!) lets just go through the full list of
  362.         simple data types (I previously simplified things).
  363.  
  364.           signed char            A  character in  the range
  365.                                  -128 to  +127,  also often
  366.                                  known simply as "char"
  367.  
  368.           unsigned char          A character in the range 0
  369.                                  to 255.
  370.  
  371.           signed short int       An  integer in   the range
  372.                                  -32768 to +32767.
  373.  
  374.           unsigned short int     An integer in  the range 0
  375.                                  to 65535.
  376.  
  377.           signed int             An integer  in  the  range
  378.                                  2147483648 to  2147483647,
  379.                                  also   known   simply   as
  380.                                  "int".
  381.  
  382.           unsigned int           An integer in  the range 0
  383.                                  to 4294987295.
  384.  
  385.           signed long int        As per "signed int".
  386.  
  387.           unsigned long int      As per "unsigned int".
  388.  
  389.           float                  A floating point number in
  390.                                  the range +/-10E-37 to +/-
  391.                                  10E+38.  A  low  precision
  392.                                  floating point number.
  393.  
  394.           double                 A floating point number in
  395.                                  the  range  +/-10E-307  to
  396.                                  +/-10E+308.
  397.                                  A high  precision floating
  398.                                  point number.
  399.  
  400.           pointer                A memory  address  in  the
  401.                                  range  0   to   0xFFFFFFFF
  402.                                  (that was a hex number, 32
  403.                                  bits worth).
  404.  
  405.         That was  all  the  possibilities  for  the Lattice
  406.         compiler.  All halfway  decent  C compilers support
  407.         the same set of  simple  types although the precise
  408.         ranges and what  "int"  and  "char"  default to may
  409.         vary.  You can expect  all  compilers for the 68000
  410.         (which the Amiga uses) would  have a similar set of
  411.         ranges and defaults.
  412.  
  413.  
  414.                           ---------------
  415.  
  416.  
  417.  
  418.         Well, thats all folks for lesson 1.  In lesson 2 we
  419.         will ponder the mysteries  of  the  full range of C
  420.         operators, complex data types and pointers.  If you
  421.         can stick with it for  that  long you deserve the C
  422.         order of merit.
  423.